package com.future.mayi.widget;

import android.content.Context;
import android.os.Build;
import android.support.v4.view.ViewCompat;
import android.support.v4.view.ViewPropertyAnimatorListener;
import android.support.v4.view.animation.FastOutSlowInInterpolator;
import android.util.AttributeSet;
import android.view.View;
import android.view.ViewGroup;
import android.view.animation.Interpolator;
import android.widget.ImageButton;

import com.future.mayi.view.xrecyclerview.XRecyclerView;

/**
 * Created by 周大虎 on 2017/12/25.
 */

public class MyFloatingButton extends ImageButton implements XRecyclerView.ScrollListener {
    private Context mContext;
    private XRecyclerView recyclerView;

    public MyFloatingButton(Context context) {
        super(context);
        init(context);
    }

    public MyFloatingButton(Context context, AttributeSet attrs) {
        super(context, attrs);
        init(context);
    }

    public MyFloatingButton(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init(context);
    }


    private void init(Context context) {
        mContext = context;
    }


    public void setRecyclerView(XRecyclerView recyclerView) {
        if (recyclerView == null) return;
        this.recyclerView = recyclerView;
        recyclerView.setScrollListener(this);
    }


    private static final Interpolator INTERPOLATOR = new FastOutSlowInInterpolator();


    private boolean inAnimation = false;

    public void animateOut() {
        if (inAnimation)
            return;
        if (getVisibility() == View.GONE)
            return;
        inAnimation = true;
        if (Build.VERSION.SDK_INT >= 14) {
            ViewCompat.animate(this).translationY(getHeight() + ((ViewGroup.MarginLayoutParams) getLayoutParams()).bottomMargin).setInterpolator(INTERPOLATOR).withLayer()
                    .setListener(new ViewPropertyAnimatorListener() {
                        public void onAnimationStart(View view) {
                            inAnimation = true;

                        }

                        public void onAnimationCancel(View view) {
                            inAnimation = false;
                        }

                        public void onAnimationEnd(View view) {
                            view.clearAnimation();
                            view.setVisibility(View.GONE);
                            inAnimation = false;
                        }
                    }).start();
        } else {
            setVisibility(View.GONE);
        }
    }

    public void animateIn() {
        if (inAnimation)
            return;
        if (getVisibility() == View.VISIBLE)
            return;
        inAnimation = true;
        setVisibility(View.VISIBLE);
        if (Build.VERSION.SDK_INT >= 14) {
            ViewCompat.animate(this).translationY(0)
                    .setInterpolator(INTERPOLATOR).withLayer().setListener(new ViewPropertyAnimatorListener() {
                @Override
                public void onAnimationStart(View view) {
                    inAnimation = true;
                }

                @Override
                public void onAnimationEnd(View view) {
                    inAnimation = false;
                }

                @Override
                public void onAnimationCancel(View view) {
                    inAnimation = false;
                }
            }).start();
        } else {
            setVisibility(View.VISIBLE);
        }
    }


    @Override
    public void scrollUp() {

    }

    @Override
    public void scrollDown() {

    }

    @Override
    public void scrollStop() {

    }
}
